package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {



	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	@Bean
	public FilterRegistrationBean<CorsFilter> corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		CorsConfiguration config = new CorsConfiguration();
		config.addAllowedOrigin("GET") ;// 允许的 HTTP 方法
		config.addAllowedOrigin("POST") ;// 允许的 HTTP 方法
		config.addAllowedOrigin("PUT") ;// 允许的 HTTP 方法
		config.addAllowedOrigin("DELETE") ;// 允许的 HTTP 方法
		config.addAllowedOrigin("OPTIONS") ;// 允许的 HTTP 方法
		config.addAllowedOrigin("http://localhost:5173"); // 允许来自特定来源的跨域请求
		config.addAllowedOrigin("http://127.0.0.1:5173/"); // 允许来自特定来源的跨域请求
		config.addAllowedOrigin("http://localhost:8888"); // 允许来自特定来源的跨域请求
		config.addAllowedOrigin("http://127.0.0.1:8888/"); // 允许来自特定来源的跨域请求
		config.addAllowedMethod("*"); // 允许所有HTTP方法
		config.addAllowedHeader("*"); // 允许所有HTTP标头
		source.registerCorsConfiguration("/**", config);

		FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
		bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
		return bean;
	}
}
